<template>

  <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="1200px"
             :close-on-click-modal="false"
             destroy-on-close
             custom-class="dialog_auto"
  >
    <el-form ref="formEl"   label-width="120px" class="common-form-panel-div"
             style="padding: 0px 10px; margin-top: 10px; overflow-y: auto ">
      <el-row>
        <el-col style="width: 100%">
          <el-form-item label="表达式注释">
            <el-input
                type="textarea"
                :rows="3"
                placeholder="请输入对表达式计费逻辑的说明"
                v-model="formValue.remark">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col style="width: 100%">
          <el-form-item label="表达式函数配置">
            <code-editor v-model="formValue.expressionFunction" height="400px"></code-editor>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="showTestData">
        <el-col style="width: 100%">
          <el-form-item label="测试数据">
            <span style="font-size: 13px;color: #807e7e">测试数据默认 10个箱子，长宽高各100cm，重100kg；10种商品，
              每种商品长宽高各10cm，重10kg，有10件；订单运费2000；命中物流分区编码为 001 ；订单总重量1000kg，总体积10立方米</span>
            <el-input
                type="textarea"
                :rows="5"
                v-model="jsonData">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>


    <div slot="footer" class="dialog-footer">

      <span v-if="showCalResult" style="
             height: 32px; line-height: 32px;
         color: #de610a; padding-right: 40px;
         font-weight: bold;
      ">
        <span style="width: 108px; text-align: right; height: 32px; line-height: 32px; ">调试费用：</span>
        <span
            style="margin-left: 12px; height: 32px; line-height: 32px;">{{ calResult }} </span>
      </span>
      <el-button @click="showTestData = !showTestData" type="text" size="mini">{{ showTestData ? '隐藏' : '显示' }}测试数据</el-button>
      <el-button @click="debug()" style="margin-right: 20px;">调试</el-button>

      <el-button @click="dialogFormVisible = false">{{ $tt('label_close') }}</el-button>
      <el-button type="primary" @click="save">{{ $tt('label_save') }}</el-button>
    </div>
  </el-dialog>

</template>


<script>

import CodeEditor from "@/component/base/code-editor.vue";
import {add} from "lodash/math";

export default {
  name: "expression-edit-dialog",
  components: {CodeEditor},

  data() {
    return {
      formValue: {
        index: 0,
        remark: '',
        expressionFunction: ''
      },

      dialogFormVisible: false,

      dialogTitle: '',
      index: 0,

      calResult: '',
      showCalResult: false,
      showTestData: false,

      jsonData: '{\n' +
          '  "orderTotalWeight": 1000.0,\n' +
          '  "orderTotalVolume": 10.0,\n' +
          '  "areaCode": "001",\n' +
          '  "orderTransportFee": 2000.0,\n' +
          '  "customerCode": "customerCode",\n' +
          '  "orderNo": "orderNo",\n' +
          '  "selfDelivery": false,\n' +
          '  "insuranceProduct": false,\n' +
          '  "calPriceByPackage": false,\n' +
          '  "orderBoxes": [\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    },\n' +
          '    {\n' +
          '      "length": 100.0,\n' +
          '      "width": 100.0,\n' +
          '      "height": 100.0,\n' +
          '      "weight": 100.0\n' +
          '    }\n' +
          '  ],\n' +
          '  "skus": [\n' +
          '    {\n' +
          '      "skuName": "skuName0",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName1",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName2",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName3",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName4",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName5",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName6",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName7",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName8",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    },\n' +
          '    {\n' +
          '      "skuName": "skuName9",\n' +
          '      "length": 10.0,\n' +
          '      "width": 10.0,\n' +
          '      "height": 10.0,\n' +
          '      "weight": 10.0,\n' +
          '      "qty": 10\n' +
          '    }\n' +
          '  ]\n' +
          '}'
    }
  },
  created() {
    var _this = this

  },

  watch: {
  },

  destroyed() {

  },

  computed: {},

  updated() {
  },

  methods: {
    debug() {

      this.$ajax({
        url: '/fms-api/customer/CustomerOrderRpc/expressionDebug.json',
        type: 'json',
        data: {
          jsonData: this.jsonData,
          expression: this.formValue.expressionFunction
        },
        success: (res) => {
          if (res && res.data) {
            this.calResult = res.data
            this.showCalResult = true
          }
        }
      });

    },
    showDialog(item,index) {

      this.dialogTitle = '表达式编辑'

      this.formValue = _.cloneDeep(item)

      this.formValue.index = index

      this.dialogFormVisible = true

    },
   save(){

     this.$emit('save',this.formValue)
     this.dialogFormVisible = false
   },


   // test
    testAouyue(){
      var fee = 0.00
      var customerCode = 'customerCode'
      var orderNo = 'orderNo'
      var orderType = ''
      var bizType = ''
      var transportProductCode = ''
      var selfDelivery = 'false'
      var insuranceProduct = 'false'
      var orderTotalWeight = 0
      var orderTotalVolume = 0
      var orderTransportFee = 2000.0
      var areaCode = ''
      var country = ''
      var province = ''
      var city = ''
      var county = ''
      var address = ''
      var postcode = ''
      var orderBoxes = []
      var skus = []


      //======== 1、 超长限制费用 ===========
      // 取物流加工包装的最大外包装长宽高数值,用 长+ 2*宽 +2*高 计算的值,校验是否超过120cm,如果超过
      // 定义 加收费用
      var addFee = 32.0
      // 数据来源有箱数据才能计算
      if (orderBoxes && orderBoxes.length > 0) {
        for (var i = 0 ; i < orderBoxes.length ; i++){
          // 实际箱数据
          var box = orderBoxes[i]
          // 只有实际数据有长宽高的时候才计算
          if (box.length && box.width && box.height){
            var boxLength = box.length
            var boxWidth = box.width
            var boxHeight = box.height
            var boxTotalLength = boxLength + 2 * boxWidth + 2 * boxHeight
            if (boxTotalLength > 120) {
              fee += addFee
            }
          }
        }
      }
      return fee

      //======== 2、 超长附加费 =========== ?

      // ①55*40*25cm，超过任一条件，需加取40元/票超长附加费；
      // ②0.453kg以内最大尺寸55*45*38cm，超过任一条件，需加取11.5元/票超长附加费；
      // ③0.453kg以上长+周长小于274cm，最长边小于55cm，超过任一条件，需加取11.5元/票超长附加费；
      // ① 满足收取条件就收钱， ②③ 任意满足收取条件，收其中一项钱

      var first = 40.0
      var second = 11.5
      var third = 11.5
      if (orderBoxes && orderBoxes.length > 0) {
        for (var i = 0 ; i < orderBoxes.length ; i++) {
          var box = orderBoxes[i]
          if (box.length && box.width && box.height && box.weight) {
            var boxLength = box.length === undefined ? 0 : box.length
            var boxWidth = box.width === undefined ? 0 : box.width
            var boxHeight = box.height === undefined ? 0 : box.height
            var boxWeight = box.weight === undefined ? 0 : box.weight
            // 最长边
            var maxBian = 0
            if (boxLength > boxWidth && boxLength > boxHeight) {
              maxBian = boxLength
            } else if (boxWidth > boxLength && boxWidth > boxHeight) {
              maxBian = boxWidth
            } else {
              maxBian = boxHeight
            }
            // 周长
            var boxAround = boxLength + boxWidth + boxHeight
            if (boxLength > 55 || boxWidth > 40 || boxHeight > 25) {
              fee += first
            }
            if (boxWeight <= 0.453 && (boxLength > 55 || boxWidth > 45 || boxHeight > 38)) {
               fee += second
              continue
            }
            if (boxWeight > 0.453 && boxAround < 274 && boxLength < 55) {
              fee += third
              continue
            }
          }
        }
        return fee
      }

      // ======== 3、 偏远地址附加费 ===========

      // 定义偏远地区编码，当命中的物流分区编码满足以下条件时，收取就加收偏远地区附加费
      var farArea = ['001', '002']
      var farFee = 100.0

      if (areaCode && farArea.indexOf(areaCode) > -1) {
        fee += farFee
        return fee
      }

      // ======== 4、超长超重费 ===========
      // 4.1 ①单件实重22KG-30KG；②包裹最长边120cm-244cm、第二长边超过76cm、围长超过266cm但不超过330cm。 收CNY180/箱
      // 4.2 Oversize(超大超尺寸费用）：30kg-68KG或长+2*（宽+高）超过330厘米且不超过419厘米的货物，       收CNY1000/箱

      var first = 180
      var second = 1000
      if (orderBoxes && orderBoxes.length > 0) {
        for (var i = 0 ; i < orderBoxes.length ; i++) {
          var box = orderBoxes[i]
          if (box.length && box.width && box.height && box.weight) {
            var boxLength = box.length === undefined ? 0 : box.length
            var boxWidth = box.width === undefined ? 0 : box.width
            var boxHeight = box.height === undefined ? 0 : box.height
            var boxWeight = box.weight === undefined ? 0 : box.weight

            // 最长边
            var maxBian = 0
            if (boxLength > boxWidth && boxLength > boxHeight) {
              maxBian = boxLength
            } else if (boxWidth > boxLength && boxWidth > boxHeight) {
              maxBian = boxWidth
            } else {
              maxBian = boxHeight
            }

            // 第二长边
            var secondBian = 0
            if (boxLength > boxWidth && boxLength < boxHeight) {
              secondBian = boxLength
            } else if (boxWidth > boxLength && boxWidth < boxHeight) {
              secondBian = boxWidth
            } else {
              secondBian = boxHeight
            }

            // 周长
            var boxAround1 = boxLength + boxWidth + boxHeight
            var boxAround2 = boxLength + (boxWidth + boxHeight) * 2

            // 4.1 满足就加收
            if ((boxWeight > 22 && boxWeight < 30) && (maxBian > 120 && maxBian < 244) && secondBian > 76 && (boxAround1 > 266 && boxAround1 < 330)) {
              fee += first
            }

            // 4.2 满足就加收
            if ((boxWeight > 30 && boxWeight < 68) || (boxAround2 > 330 && boxAround2 < 419)) {
              fee += second
            }

          }
        }
        return fee
      }


      // ======== 5、 多品名附加费：单票超过5个申报品名，每增加一个加收RMB30 ===========
      var addFee = 30.0
      if (skus && skus.length > 5) {
        fee += (skus.length - 5) * addFee
        return fee
      }

      // ======== 6、 燃油附加费 ==================
      // 举例:燃油附加费按照订单的5%收取,订单运费100元,燃油附加费100*0.05=5元
      var percent = 0.05
      if (orderTransportFee) {
        fee += orderTransportFee * percent
        return fee
      }

      // ======== 7、异形货附加费 ===========
      // 当单品最长边>2.4m时视作为异性货物,这里也是需要逐一去校验货物的长宽高中有没有哪个尺寸超过限制长度

      // 定义异形货物的加收费用
      var addFee = 100.0
      if (orderBoxes && orderBoxes.length > 0) {
        for (var i = 0; i < orderBoxes.length; i++) {
          var box = orderBoxes[i]
          var boxLength = box.length === undefined ? 0 : box.length
          var boxWidth = box.width === undefined ? 0 : box.width
          var boxHeight = box.height === undefined ? 0 : box.height
          // 该箱子的长宽高有一个超过240cm,就加收费用
          if (boxLength > 240 || boxWidth > 240 || boxHeight > 240) {
            fee += addFee
          }
        }
        return fee
      }

      // ====== 8、超限制附加费 ================
      // 定义限制分区编码，当实际命中的物流分区满足是限制分区时，就加收超限制附加费
      var farArea = ['001', '002']
      var farFee = 100.0
      if (areaCode && farArea.indexOf(areaCode) > -1) {
        fee += farFee
        return fee
      }

      // ====== 9、一票多件 ================
      // 一票多件，当出库SKU单品数量＞1时，第二个单品开始按设定的重量价格配置段价格收取，且是按照单个件的重量进行收取

      // 第一段续重每1 KG 续重价格
      var firstWeightPrice = 0.16
      // 定义第一件重量价格配置段
      var firstWeightRange = [
        {start: 0, end: 1, price: 0.42},
        {start: 1, end: 3, price: 0.54},
        {start: 3, end: 5, price: 0.69},
        {start: 5, end: 10, price: 1.18}
      ]
      // 定义第二件重量价格配置段
      var secondWeightRange = [
        {start: 0, end: 1, price: 0.30},
        {start: 1, end: 3, price: 0.40},
        {start: 3, end: 5, price: 0.45},
        {start: 5, end: 10, price: 0.84},
        {start: 10, end: 999, price: 1.28}
      ]

      if (skus && skus.length > 0){
        for (var i = 0 ; i < skus.length ; i++){
          var sku = skus[i]
          // 商品重量
          var skuWeight = sku.weight === undefined ? 0 : sku.weight
          // 商品数量
          var qty = sku.qty === undefined ? 0 : sku.qty

          // 第一种商品
          if (i === 0){
            // 第一件的价格
            var matchPrice = 0
            for (var j = 0 ; j < firstWeightRange.length ; j++){
              var range = firstWeightRange[j]
              // 最后一段区间，超过算续重
              if (j == firstWeightRange.length - 1){
                matchPrice = range.price
                break
              }
              if (skuWeight > range.start && skuWeight <= range.end){
                // 命中区间价格
                matchPrice = range.price
                break
              }
            }
            // 续重 = 商品重量 - 最后一段区间的结束值
            var addFee = 0
            var addWeight = skuWeight - firstWeightRange[firstWeightRange.length - 1].end
            if (addWeight > 0){
              var tmpAddWeight = addWeight
              // 续重不足1kg,算1kg
              var r = addWeight % 1
              if (r > 0 && r < 1){
                tmpAddWeight = addWeight + 1
              }
              addFee = tmpAddWeight * firstWeightPrice
            }

            // 首重价格
            var fisrtFee = matchPrice * (skuWeight - addWeight)
            // 单品单件商品费用 = 首重 + 续重
            var singleFee = fisrtFee + addFee
            // 总费用 = 单件费用 * 数量
            var tmpFee = singleFee * qty

            fee += tmpFee

          } else {

            // 第二种商品起命中价格
            var matchPrice = 0
            for (var j = 0 ; j < secondWeightRange.length ; j++){
              var range = secondWeightRange[j]
              if (j == secondWeightRange.length - 1){
                matchPrice = range.price
                break
              }
              if (skuWeight > range.start && skuWeight <= range.end){
                matchPrice = range.price
                break
              }
            }
            // 第二种商品的费用
            var secondFee = matchPrice * skuWeight * qty
            fee += secondFee
          }
        }
        return fee
      }








    },


    testIML() {
      var fee = 0.00
      var customerCode = 'customerCode'
      var orderNo = 'orderNo'
      var orderType = ''
      var bizType = ''
      var transportProductCode = ''
      var selfDelivery = 'false'
      var insuranceProduct = 'false'
      var orderTotalWeight = 0
      var orderTotalVolume = 0
      var orderTransportFee = 2000.0
      var areaCode = ''
      var country = ''
      var province = ''
      var city = ''
      var county = ''
      var address = ''
      var postcode = ''
      var orderBoxes = []
      var skus = []


      //======== 1、 出库包材费 ===========
      // 包材费计算方式：一票一件的话，包材费为0；一票多件包材费为2元，可编辑金额
      // 定义包材费
      var packFee = 2.0

      // 计算订单数据的商品数据，算出商品总数
      var skuTotal = 0
      if (skus && skus.length > 1){
        for (var i = 0 ; i < skus.length ; i++){
          var sku = skus[i]
          var qty = sku.qty === undefined ? 0 : sku.qty
          skuTotal += qty
        }
      }
      if (skuTotal > 1){
        fee = packFee
      }
      return fee


      //======== 2、 出库操作费 ===========
      // 操作费计算方式，按一个订单内SKU件数 ，1件不收费 ，每多一件加1元，最低收费2元，最高收费5元，可编辑金额
      // 定义最低收费
      var minFee = 2.0
      // 定义最高收费
      var maxFee = 5.0
      // 定义实际收费
      var actFee = 0.0

      var totalSkuNum = 0
      // 计算订单数据的商品数据，算出商品总数
      if (skus && skus.length > 1){
          for (var i = 0 ; i < skus.length ; i++){
              var sku = skus[i]
              var qty = sku.qty === undefined ? 0 : sku.qty
              totalSkuNum  = totalSkuNum + qty
          }
      }
      if (totalSkuNum < 2){
          return 0
      }
      actFee = totalSkuNum - 1
      // 收费要在最低与最高之间
      if (actFee < minFee){
        fee = minFee
      } else if (actFee > maxFee){
        fee = maxFee
      }
      return fee
    }
















  },
  props: {

  }






}
</script>



<style scoped>

</style>
